using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Domain.Entities;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.CmsDbContext;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Repositories
{
    /// <summary>
    /// 发布仓储实现
    /// 提供内容发布和发布历史的数据访问功能实现，使用内存存储
    /// </summary>
    public class PublishRepository : IPublishRepository
    {
        private readonly LowCodeDbContext _context;
        /// <summary>
        /// 发布记录集合
        /// </summary>
        private readonly List<Publish> _publishes = new();

        public PublishRepository(LowCodeDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 发布内容
        /// 将指定内容发布到目标环境，创建发布记录
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <param name="publishTime">发布时间，如果为null则使用当前UTC时间</param>
        /// <param name="targetEnvironment">目标发布环境，可选参数</param>
        /// <returns>发布操作的异步任务</returns>
        public async Task PublishAsync(long contentId, string? publishTime = null, string? targetEnvironment = null)
        {
            await Task.CompletedTask;
            var publish = new Publish
            {
                //Id =long.NewGuid().ToString(),
                ContentId = contentId,
                Status = "published",
                PublishedAt = publishTime != null ? DateTime.Parse(publishTime) : DateTime.UtcNow,
                CreatedAt = DateTime.UtcNow
            };
            _publishes.Add(publish);
        }

        /// <summary>
        /// 撤销发布内容
        /// 将指定内容从发布状态撤回
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <returns>撤销发布操作的异步任务</returns>
        public async Task UnpublishAsync(long contentId)
        {
            await Task.CompletedTask;
            var publish = _publishes.LastOrDefault(p => p.ContentId == contentId && p.Status == "published");
            if (publish != null) publish.Status = "unpublished";
        }

        /// <summary>
        /// 分页获取发布历史记录
        /// 获取指定内容的发布操作历史
        /// </summary>
        /// <param name="contentId">内容标识符</param>
        /// <param name="page">页码，默认为1</param>
        /// <param name="limit">每页数量，默认为10</param>
        /// <returns>发布历史记录列表</returns>
        public async Task<IEnumerable<Publish>> GetPublishHistoryAsync(long contentId, int page = 1, int limit = 10)
        {
            await Task.CompletedTask;
            return _publishes.Where(p => p.ContentId == contentId)
                .OrderByDescending(p => p.PublishedAt)
                .Skip((page - 1) * limit)
                .Take(limit);
        }

        /// <summary>
        /// 分页获取发布队列
        /// 获取待发布或已发布的内容队列
        /// </summary>
        /// <param name="status">状态筛选条件，如果为null则获取所有状态</param>
        /// <param name="page">页码，默认为1</param>
        /// <param name="limit">每页数量，默认为10</param>
        /// <returns>发布队列列表</returns>
        public async Task<IEnumerable<Publish>> GetPublishQueueAsync(string? status = null, int page = 1, int limit = 10)
        {
            await Task.CompletedTask;
            var query = _publishes.AsQueryable();
            if (!string.IsNullOrEmpty(status))
                query = query.Where(p => p.Status == status);
            return query.OrderByDescending(p => p.CreatedAt)
                .Skip((page - 1) * limit)
                .Take(limit);
        }
    }
}
